package com.ysd.service.impl;

import java.util.List;
import java.util.Optional;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.ysd.entity.Users;
import com.ysd.repository.UserRepositpry;
import com.ysd.service.UserService;
@Service
public class userServiceImpl implements UserService {
@Autowired 
private UserRepositpry userRepository;
	@Override
	public Users findByadmin(String login_name, String password) {
		// TODO Auto-generated method stub
		return userRepository.findByadmin(login_name, password);
	}
	@Override
	public Page<Users> findAllUsers(Users u, Pageable pageable) {
		// TODO Auto-generated method stub
		return userRepository.findAll(get(u),pageable);

}
	
	
		private Specification<Users> get(Users u) {
		// TODO Auto-generated method stub
		return new Specification<Users>() {
			
			@Override
			public Predicate toPredicate(Root<Users> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
				// TODO Auto-generated method stub
				Predicate predicate = criteriaBuilder.conjunction();
				List<Expression<Boolean>> expressions = predicate.getExpressions();
				if (u.getLoginName()!=null && u.getLoginName()!="") {
					expressions.add(criteriaBuilder.like(root.get("LoginName"),"%"+u.getLoginName()+"%" ));
					}
				if (u.getProtectMTel()!=null && u.getProtectMTel()!="") {
					expressions.add(criteriaBuilder.like(root.get("protectMTel"),"%"+u.getProtectMTel()+"%" ));
					}
				if (u.getProtectEMail()!=null && u.getProtectEMail()!="") {
					expressions.add(criteriaBuilder.like(root.get("ProtectEMail"),"%"+u.getProtectEMail()+"%" ));
					}
				return predicate;
			}
		};
	}


		

	@Override
	@Transactional
	public void deleteUser(Users users) {
		// TODO Auto-generated method stub
		userRepository.delete(users);
	}
	@Override
	@Transactional
	public Users addAndUpdate(Users s) {
		// TODO Auto-generated method stub
		return userRepository.saveAndFlush(s);
	}
	@Override
	public List<Users> findAllUsers() {
		// TODO Auto-generated method stub
		return userRepository.findAll();
	}
	@Override
	public Optional<Users> findByIDfindRoles(String userid) {
		// TODO Auto-generated method stub
		return   userRepository.findById(userid);
	}
	/**
	 *重置密码为 ‘admin’
	 */
	@Override
	public Integer uppassword(String id) {
		// TODO Auto-generated method stub
		return userRepository.uppassword(id);
	}
   //-----------------------------------权限登录------------------------------------------
	@Override
	public Users findUsersById(String Id) {
		// TODO Auto-generated method stub
		return userRepository.findById(Id).get();
	}
	
	
}

